<?php
	include_once("king_head.php");
	include_once("ClsSebent/config.php");
	include_once("ClsSebent//Content_class.php");	
	
	$page_option=new Content_class();
	
	
	
	#$page_option->showPageOption($_SESSION['SITE_ID']);
		

	
function newlink($D, $num, $site_id)
{ 
			$page_option=new Content_class();
		?>
		
		<tr>
		<td><?php echo $num; ?></td>
		<td><input name="link_text_x<?php echo $D; ?>"></td>
		<td>
		<select name="link_page_x<?php echo $D; ?>">
		<option value='0'>Not Linked</option>
		<option value='10'>Product Page</option>
		</select>
		</td>
		<td><select name="link_parent_x<?php echo $D; ?>">
		<?php	
			
			$page_option->showPageOption($_SESSION['SITE_ID']);
		?>
		</select></td>
		<td><input type='checkbox' name="link_active_x<?php echo $D; ?>" value='Y' checked></td>
		<td><input name="link_position_x<?php echo $D; ?>" value='Z'></td>
		</tr>
		<?php
}

function add_products_link($lp, $st, $lnktxt, $actv, $lpos)//link parent and site id passed.
{
	if($lp!=0)
	{
		$q="select link_text from site_navbar where id=".$lp;
		$r=mysql_query($q);
		$row=mysql_fetch_array($r);
		$txt_lnk=mysql_escape_string($row['link_text']);
		$q1="select id from site_navbar where site_id=".$st." and link_text='$txt_lnk'";
		//echo '<BR>'.$q1;
		$r1=mysql_query($q1);
		$ro1w=mysql_fetch_array($r1);
		$lnk_par_i=$row1['id'];
	}
	else 
		$lnk_par_i=0;
		$p_qins="INSERT INTO `site_navbar` (`link_text`, `site_id`, `link_page`, `link_page_title`, `link_parent`, `link_active`, `link_position`, `admin_lnk`) values('".$lnktxt."', '".$st."', '10', 'Product Page', '".$lnk_par_i."', '".$actv."', '".$lpos."', 'Y')";		
		mysql_query($p_qins);		
}


function updt_products_link($lpx, $st, $lnktxt, $actv, $lpos, $linkid)//link parent and site id passed.
{ //echo '<br>GOT IN PRODUCT LINK UPDATE';
	if($lpx!=0)
		{
		$qx="select link_text from site_navbar where id=".$lpx;
		$rx=mysql_query($qx);
		$rox=mysql_fetch_array($rx);
		$txtx=mysql_escape_string($rox['link_text']);
		$q1x="select id from site_navbar where site_id=".$st." and `link_text`='".$txtx."'";
		$r1x=mysql_query($q1x);
		$ro1x=mysql_fetch_array($r1x);
		$lnk_par_ix=$ro1x['id'];
		if($lnk_par_ix=="")
		$lnk_par_ix=0;
		}
		else 
		$lnk_par_ix=0;
		
		//GET the link id to update.
		$glt=mysql_escape_string($lnktxt);
		
		$p_qinsx="UPDATE `site_navbar` SET `link_text`='$glt', link_page='10', link_parent='$lnk_par_ix', link_active='$actv', link_position='$lpos' WHERE id='$linkid'";
		mysql_query($p_qinsx);	
		//if($st==104) echo'<BR>'.$p_qinsx;	
		
}

function res_parentlink($pid, $st) 
{		
		//echo "ADMIN PAR_ID: $pid";
		$q2="select * from `site_navbar` where `id`=".$pid;
		//echo $q2;
		$r2=mysql_query($q2);
		$row2=mysql_fetch_array($r2);
		$txt=mysql_escape_string($row2['link_text']);
		$q3="select id from `site_navbar` where `site_id`=".$st." and link_text='".$txt."'";
		//echo $q3;
		$r3=mysql_query($q3);
		$row3=mysql_fetch_array($r3);
		if($row3['id']=='')
		return 0;
		else
		$result=$row3['id'];
		//echo $result;
		return $result;		
}

function lnk_to_updt($ltxt, $st)
{
					$qlink_text=mysql_escape_string($ltxt);
					$qup="select * from site_navbar where site_id=".$st." and link_text='".$qlink_text."'";
					//echo"<BR>QId ".$qup;
					$rup=mysql_query($qup);
					$rowup=mysql_fetch_array($rup);
					$linkid=$rowup["id"];
					//echo $linkid;
					if($linkid=="")
					return 0;
					else 
					return $linkid;
}
?>

<H2> Navbar Control Panel</H2><BR>
The links appearing in <span style="color: #FF0000">Red Text</span> are the links created by Admin.
<form method='post' action="">
<table width="90%" align="center">
 <tr>
					  <td></td>
					  <td>Add New Link(s)</td>
					  <td><input name='linknum' type="text" size="3" /></td>
					  <td><input type="submit" name="action" value="Save" /></td>
					  <td>&nbsp;</td>
					  <td><input type="submit" name="action" value="Add new link(s)" /></td>
					  <td></td>
    </tr>
</table>

<TABLE border='1' width='90%' align="center">
  
	<TR>
	<input type='hidden' name='site_id' value="<?php echo $_SESSION['SITE_ID']; ?>">


		<TH>S.No.</TH>
		<TH>Link Text</TH>
		<TH>Link Page</TH>
		<TH>Link is Sublink to</TH>
		<TH>Display Link</TH>
		<TH>Link Order for Display</TH>
		<TH>Click to Remove</TH>
	</TR>
<?php
	
	$link_page_title="";
	
	
	
	if(($_REQUEST['action']=="Save") && $_SESSION['SITE_ID'] != "")
	{
//ADMIN SITE SAVE.
		$D=$_REQUEST['D'];
		$num=$_REQUEST['num'];

#		for($i=1; $i<=$num-$D; $i++)
		for($i=1; $i<=$num-$D; $i++)
		{
			${'link_page' . $i}=$_REQUEST['link_page'.$i];
			#echo $_REQUEST['link_page1'];
			${'link_text' . $i}=$_REQUEST['link_text'.$i];
			${'link_parent' . $i}=$_REQUEST['link_parent'.$i];
			${'link_active' . $i}=$_REQUEST['link_active'.$i];
			${'link_position' . $i}=$_REQUEST['link_position'.$i];
			${'id' . $i}=$_REQUEST['id'.$i];
			$qst="select page_title from site_page where id='".${'link_page' . $i}."'";
			//echo "<BR>Qx " . $qs0;
			$rst=mysql_query($qst);
			$rect=mysql_fetch_array($rst);			
			$link_page_title=mysql_escape_string($rect["page_title"]);
			$n_link_text=${'link_text' . $i};
			$n_link_text=mysql_escape_string(${'link_text' . $i});
			if((${'link_page'.$i})==10) $link_page_title='Product Page';
			$sql="UPDATE site_navbar SET link_text='".$n_link_text."', link_page='".${'link_page' . $i}."', link_page_title = '".$link_page_title."', link_parent='".${'link_parent' . $i}."', link_active='".${'link_active' .$i}."', link_position='".${'link_position' . $i}."' WHERE id=".${'id' . $i};
			#echo "<BR>QS ".$sql;

			mysql_query($sql);
			#echo mysql_error();
		}
		//*****IF NEW RECORD(S) INSERTED************
#		for($p=1; $p<=$D; $p++)
		for($p=1; $p<=$D; $p++)
		{
						//echo ${'link_text_x'.$p};

			${'link_text_x' . $p}=$_REQUEST['link_text_x'.$p];
			${'link_page_x' . $p}=$_REQUEST['link_page_x'.$p];
			${'link_parent_x' . $p}=$_REQUEST['link_parent_x'.$p];
			${'link_active_x' . $p}=$_REQUEST['link_active_x'.$p];
			${'link_position_x' . $p}=$_REQUEST['link_position_x'.$p];



			if(!(${'link_text_x'.$p}==""))
			{				
				$qst="select page_title from site_page where id='".${'link_page_x'.$p}."'";
				//echo "<BR>Q0 " . $qst;
				$rst=mysql_query($qst);
				$rect=mysql_fetch_array($rst);
				$link_page_title=$rect["page_title"];
				$link_page_title=mysql_escape_string($link_page_title);
				$nlink_text=${'link_text_x'.$p};
				$nlink_text=mysql_escape_string($nlink_text);

				$sql="INSERT INTO site_navbar (link_text, site_id, link_page, link_page_title, link_parent, link_active, link_position) values('".$nlink_text."', '".$_SESSION['SITE_ID']."', '".${'link_page_x'.$p}."', '".$link_page_title."', '".${'link_parent_x'.$p}."', '".${'link_active_x'.$p}."', '".${'link_position_x'.$p}."')";
				/*echo ${'link_text_x'.$p}."<BR>";
				echo $_SESSION['SITE_ID']."<BR>";
				echo $link_page_title."<BR>";
				echo ${'link_parent_x'.$p}."<BR>";
				echo ${'link_active_x'.$p}."<BR>";
				echo ${'link_position_x'.$p}."<BR>";*/
				#echo "<BR>QI ".$sql;
				mysql_query($sql);
				#echo mysql_error();

			}
		}		
//ADMIN SITE SAVE END.###########################################################################################

//GLOBAL CHANGES TO RESELLERS.
if($_SESSION['SITE_ID']==101)
{	
			//SELECT "ON" RESELLERS.
			$xqry="select site_id from reseller where updates='Y'";
			$xres=mysql_query($xqry);
			$D=$_REQUEST['D'];
			while($xrow=mysql_fetch_array($xres))
			{
				$sid=$xrow["site_id"];
				if($sid==0) continue;
				

				$D=$_REQUEST['D'];
				//EXISTING LINKS SAVE WITH UPDATE. i TO num. 
				for($i=1; $i<=$num-$D; $i++)
				{		
					$gltx=mysql_escape_string(${'link_text'.$i});
					$ilinkpage=${'link_page'.$i};
					$d_q="select * from site_navbar where link_text='$gltx' and site_id=$sid"; 
					//echo 'First query:'.$d_q.'<BR>'; 
					$d_r=mysql_query($d_q);
					$d_ro=mysql_fetch_array($d_r);					
					//IF LINK TEXT IS NOT EXISTING IN RESELLER(LINK IS DELETED). Needs to insert the link instead of updation.
					if($d_ro['link_text']=='')
					{				//echo 'LINK'.$gltx.'Not found on site '.$sid.'<BR>'; 
					//SPECIAL CASE PRODUCTS PAGE.
					 //echo $ilinkpage;
						if($ilinkpage==10)
						{
						//echo'Entered special case.'; 
						add_products_link(${'link_parent'.$i}, $sid, ${'link_text'.$i}, ${'link_active'.$i}, ${'link_position'.$i});
						continue;
						}
					//PRODUCT PAGE END.
						
						if(($ilinkpage!=10) && ($ilinkpage!=0))
						{//insert the link to reseller IF NOT EXISTING.
						//GET LINK PAGE id.
						//echo'Try to insert link '.$gltx;
						$lpq="select page_title from site_page where id=".$ilinkpage;
						$lpr=mysql_query($lpq);
						$lprow=mysql_fetch_array($lpr);
						$link_p_title=mysql_escape_string($lprow['page_title']);
						$d_q1="select id from site_page where page_title='".$link_p_title."' and site_id='$sid'"; 
						//echo '<BR> '.$d_q1;
								$d_r1=mysql_query($d_q1);
								$d_ro1=mysql_fetch_array($d_r1);
								if($d_ro1['id']=='')//IF page do not exits, skip the link.
								continue;
								else//IF page exists.
								{
								$link_page_d=$d_ro1['id'];
								//echo'<BR>GOT LINK ID: '.$link_page_d;
								//NOW GET LINK PARENT id also.								
								$adpar=${'link_parent'.$i};								  
									if($adpar!=0)
									{
									//echo'<BR> ADMIN PAR: '.$adpar;
									//echo'<BR> **PRONE**';
									$link_par_d=res_parentlink($adpar, $sid);	
									if($link_par_d==0) continue;
									}
									else
									$link_par_d=0;								
								}							
								//Insert the link now
								$d_qins1="INSERT INTO site_navbar (link_text, site_id, link_page, link_page_title, link_parent, link_active, link_position, admin_lnk) values('$gltx', '$sid', '$link_page_d', '$link_p_title', '$link_par_d', '${'link_active'.$i}', '${'link_position'.$i}', 'Y')";	//echo $d_qins;
								mysql_query($d_qins1);//LINK INSERTED.
								continue;
						}//IF LINK PAGE != 10
					}//IF LINK NOT EXISTING PROCESS CLOSED.
				
				
				//GENERAL SAVE UPDATE PROCESS. IF LINK TEXT IS EXISTING.
				//echo $num.'****'.$D.'<BR>';
				
				//SPECIAL CASE PRODUCTS PAGE LINK.
						if($ilinkpage==10)
						{ //if($sid==104) echo '<BR> PRODUCT PAGE UPDATE '.$gltx.'  '.$sid;
						//get link to update.
						$linkid=lnk_to_updt($gltx, $sid);
						if($linkid==0)
						continue;
						
					updt_products_link(${'link_parent'.$i}, $sid, $gltx, ${'link_active' .$i}, ${'link_position'.$i}, $linkid);
					continue;							
						}
				//PRODUCT PAGE LINK UPDATED.
				
				//OTHER PAGES TO UPDATE.
					if (($ilinkpage!=0) && ($ilinkpage!=10))
					{  //if($sid==104) echo '<BR>Link: '.$gltx;
						$qs0="select page_title from site_page where id=".$ilinkpage;
						//if($sid==104)echo "<BR>LQ0 " . $qs0;
						$rs0=mysql_query($qs0);
						$rec0=mysql_fetch_array($rs0);						
						$page_title_res=mysql_escape_string($rec0["page_title"]);
						//echo $page_title_res;
//Get the linkpage.
						$qs1="select * from site_page where site_id=".$sid." and page_title='$page_title_res'";
						//if($sid==104)echo "<BR>LQ1 " . $qs1;
						$rs1=mysql_query($qs1);
						$rec1=mysql_fetch_array($rs1);
						$link_page1=$rec1["id"];						
						if($link_page1=="")
							continue;
					}				
					
//Get the linkparent. 
					$nlinkparent=${'link_parent'.$i}; //if(site_id==104)echo '<BR>LP: '.$nlinkparent.'* ';
					if ($nlinkparent!=0)
					{
					$nlinkpar=res_parentlink($nlinkparent, $sid);
						if($nlinkpar==0)
						continue;
					}
					else 
					$nlinkpar=0;					

//Get the link id to be updated.
					$nlinkid=lnk_to_updt($gltx, $sid);
					if($nlinkid==0)
						continue;
					
					$sql="UPDATE site_navbar SET link_text='$gltx', link_page='$link_page1', link_parent='$nlinkpar', link_active='${'link_active' .$i}', link_position='${'link_position' . $i}' WHERE id='$nlinkid'";
					//if($sid==104)echo "<BR>OPAGE UPDATE: ".$sid."* " . $sql;
					mysql_query($sql); //echo'GENERAL SAVE :'.$glink_text.'<BR>';
				}//FOR..
				//GENERAL SAVE FINISHED.
								
				//*****IF NEW RECORD INSERTED GLOBAL************
				//echo "<BR>Insertion for Site Id: ".$sid;				
					for($p=1; $p<=$D; $p++)
					{		
							$lt=mysql_escape_string(${'link_text_x'.$p});
							$lp=mysql_escape_string(${'link_page_x'.$p});
							
							//SPECIAL CASE PRODUCTS PAGE.
							if($lp==10)
							{
							//check for the link parent.
							$lnkpar=${'link_parent_x'.$p};
							if($lnkpar!=0)
							{
							$lnkpar=res_parentlink($lnkpar, $sid);
							if($lnkpar==0)
							continue;
							}
							else 
							$lnkpar=0;
							//echo '<BR>RE_PAR: '.$lnkpar;
							$xsql="INSERT INTO site_navbar (link_text, site_id, link_page, link_page_title, link_parent, link_active, link_position, admin_lnk) values('$lt', '$sid', 10, 'Product Page', $lnkpar, '${'link_active_x'.$p}', '${'link_position_x'.$p}', 'Y')";
								//echo "<BR>GIQ4 ".$xsql;
								mysql_query($xsql);
								continue;
							}
							
							if($lt!="" && $lp!=10)
							{//echo'GENERAL NEW LINK.';
								$qstx="select page_title from site_page where id=".$lp;
								//echo "<BR>GQ0 " . $qstx;
								$rstx=mysql_query($qstx);
								$rectx=mysql_fetch_array($rstx);								
								$link_page_title=mysql_escape_string($rectx["page_title"]);
								$qsI="select id from site_page where site_id=".$sid." and page_title='".$link_page_title."'";
								//echo "<BR>Q INS " . $qsI;
								$rsI=mysql_query($qsI);
								$rowI=mysql_fetch_array($rsI);					
									$link_pageI=$rowI["id"]; 
									if($link_pageI=="")
									continue;									

								//get link parent
								$lpar=${'link_parent_x'.$p};
									if($lpar!=0)
									{
										$qsp="select link_text from `site_navbar` where `id`=".$lpar;
										$rsP=mysql_query($qsp);
										$rowP=mysql_fetch_array($rsP);
										$Plink_text=mysql_escape_string($rowP["link_text"]);
										$ssq="select id from site_navbar where site_id=".$sid." and link_text='".$Plink_text."'";
								//echo '<BR>'.$ssq;
										$ssr=mysql_query($ssq);
										$ssrow=mysql_fetch_array($ssr);
										$Plink_parent=$ssrow["id"];	
										if($Plink_parent=="");
										continue;							
									}
									else 
									$Plink_parent=0;																			
								$xsql="INSERT INTO site_navbar (link_text, site_id, link_page, link_page_title, link_parent, link_active, link_position, admin_lnk) values('$lt', '$sid', $link_pageI, '$link_page_title', '$Plink_parent', '${'link_active_x'.$p}', '${'link_position_x'.$p}', 'Y')";
								//echo "<BR>GIQ4 ".$xsql;
								mysql_query($xsql);
							}
					}//FOR LOOP NEW RECORD GLOBAL.

			} //End of WHILE loop over resellers

		} //End of IF check*********** site id=101
		
	} // End of action on SAVE button**************

	if($_REQUEST['action']=="del" && $_SESSION['SITE_ID'] != "")
	{		
	$page_option=new Content_class();
		$link_id=$_REQUEST['link_id'];
		//*************global changes
		if($_SESSION['SITE_ID']==101)
		{	
			

			$lnqry="select link_text from site_navbar Where id=".$link_id;
			$lnres=mysql_query($lnqry);
			$lntext=mysql_fetch_array($lnres);

			$xqry="select site_id from reseller where updates='Y'";
			$xres=mysql_query($xqry);
			
			while($xrow=mysql_fetch_array($xres))
			{
				$sid=$xrow["site_id"];
				$ltxt=mysql_escape_string($lntext["link_text"]);

				//delete the child links first.
				$chq="select * from site_navbar where site_id=".$sid." and link_text='".$ltxt."'";
				//echo $chq.'<BR>';
				$chr=mysql_query($chq);
				if($chrow=mysql_fetch_array($chr))
				{$xlinkid=$chrow["id"];
				$chdel="delete from site_navbar where link_parent=".$xlinkid;}
				//echo $chdel.'<BR>';
				mysql_query($chdel);
				//delete the parent link.								
				$uqry="delete from site_navbar where id=".$xlinkid;
				//echo $uqry;		
				mysql_query($uqry);
			}
		}
		//*********				
		$sqlx="delete from site_navbar where link_parent=".$link_id;
		mysql_query($sqlx);
		$sql="DELETE FROM site_navbar WHERE id=".$link_id;
		//echo $sql;
		mysql_query($sql);		
	}

	#$sql="SELECT * FROM `site_navbar` WHERE `site_id`='".$_SESSION['SITE_ID']."' and `link_parent`='0' ORDER BY `link_position`, `link_text`";
	$sql="SELECT * FROM `site_navbar` WHERE `site_id`='".$_SESSION['SITE_ID']."' ORDER BY `link_position`, `link_text`";
	//echo "<BR>".$sql;
	$result = mysql_query($sql);
	$num=1;
	
	//echo "rows = ".mysql_num_rows($result)."<br>";
	
	while($xrow=mysql_fetch_array($result,MYSQL_ASSOC))
		{
		
		//print_r($xrow);
			
	//	echo "<BR>".$xrow["id"];
		//PRINT PARENT LINK
					$link_id=$xrow["id"];
					$link_text=mysql_escape_string($xrow["link_text"]);
					$link_page=$xrow["link_page"];
					$link_parent=$xrow["link_parent"];
					$link_active=$xrow["link_active"]; 
					$link_position=$xrow["link_position"];
					$admin_lnk=$xrow["admin_lnk"];
					if($admin_lnk=='Y')
					$style='style="color:#FF0000; font-weight:bold;"';
					else
					$style='';
					
				/*	echo "<tr>";
					echo "<td><input type='hidden' name=id$num value=$link_id>" . $num . "</td>";
					echo "<td><input name=link_text$num $style value=\"" . $link_text . "\"></td>";
					echo "<td><select name=link_page$num><option value=0>Not Linked";
					showPageOption($_SESSION['SITE_ID'],$link_page);
					if("$link_page"=="10") $product_page_selected="selected";
					else  $product_page_selected="";
					echo "<option value=10 $product_page_selected>Product Page</select></td>";
					echo "<td><select name=link_parent$num>";
					showParentOption($_SESSION['SITE_ID'],$link_parent);
					echo "</select></td>";
					if($link_active == "Y") $set="checked";
					else $set="";
					echo "<td><input type=checkbox value=Y name=link_active$num $set></td>";
					echo "<td><input name=link_position$num value='" . $link_position . "'></td>";
					echo "<td><a href=navbar.php?action=del&site_id=".$_SESSION['SITE_ID']."&link_id=" . $link_id . ">Delete</a></td>";
					echo "</tr>";*/
					
						?>
					<tr>
					<td><input type='hidden' name="id<?php echo $num;?>" value="<?php echo $link_id;?>"><?php echo $num; ?></td>
					<td><input name="link_text<?php echo $num; ?>" <?php echo  $style; ?> value="<?php echo  $link_text ; ?>"></td>
					<td>
					<select name="link_page<?php echo $num; ?>">
					<option value='0'>Not Linked</option>
					<?php 
					
					$page_option->showPageOption($link_page); 
					
					if($link_page=="10") $product_page_selected="selected";
					else  $product_page_selected="";
					
					?>
					<option value='10' <?php echo $product_page_selected;?> >Product Page</option>
					</select>					</td>
					<td>
					
					<select name="link_parent<?php echo $num; ?>" >
					<option value="0"></option>
					<?php
							#$page_option->showPageOption($_SESSION['SITE_ID'],$link_page); 
							$page_option->showPageOption($link_parent); 
					?>
					</select>					</td>
					<?php
					
					if($link_active == "Y") $set="checked";
					else $set="";
					?>
					<td><input type='checkbox' value='Y' name="link_active<?php echo $num; ?>" <?php echo  $set; ?> ></td>
					<td><input name="link_position<?php echo $num; ?>" value="<?php echo $link_position; ?>"></td>
					<td><a href="navbar.php?action=del&site_id=<?php echo $_SESSION['SITE_ID']; ?>&link_id=<?php echo $link_id; ?>">Delete</a></td>
					</tr>
					<?php
					
					$num=$num+1;
		//------------------NESTED LINKS------------
		$nested="SELECT * FROM `site_navbar` WHERE `site_id`='".$_SESSION['SITE_ID']."' AND `link_parent`=" . $xrow["id"] . " ORDER BY `link_position`, `link_text`";
		//echo "<br>\$nested=".$nested;
		$nes=mysql_query($nested);
		
		while($row=mysql_fetch_array($nes,MYSQL_ASSOC))
			{
					$link_id=$row["id"];
					$link_text=mysql_escape_string($row["link_text"]);
					$link_page=$row["link_page"];
					$link_parent=$row["link_parent"];
					$link_active=$row["link_active"];
					$link_position=$row["link_position"];
					$admin_lnk=$row["admin_lnk"];
					if($admin_lnk=='Y')
					$style='style="color:#FF0000; font-weight:bold;"';
					else
					$style='';
					/*
					echo "<tr>";
					echo "<td><input type=hidden name=id$num value=$link_id>" . $num . "</td>";
					echo "<td><input name=link_text$num $style value=\"" . $link_text . "\"></td>";
					echo "<td><select name=link_page$num><option value=0>Not Linked";
					showPageOption($_SESSION['SITE_ID'],$link_page);
					$page_option->showPageOption($_SESSION['SITE_ID']);
					if("$link_page"=="10") $product_page_selected="selected";
					else  $product_page_selected="";
					echo "<option value=10 $product_page_selected>Product Page</select></td>";
					echo "<td><select name=link_parent$num>";
					showParentOption($_SESSION['SITE_ID'],$link_parent);
					echo "</select></td>";

					if($link_active == "Y") $set="checked";
					else $set="";
					echo "<td><input type=checkbox value=Y name=link_active$num $set></td>";
					echo "<td><input name=link_position$num value='" . $link_position . "'></td>";
					echo "<td><a href=navbar.php?action=del&site_id=".$_SESSION['SITE_ID']."&link_id=" . $link_id . ">Delete</a></td>";
					echo "</tr>";*/
					?>
					<tr>
					<td><input type='hidden' name="id<?php echo $num; ?> value="<?php echo $link_id; ?> ><?php echo $num; ?></td>
					<td><input name="link_text<?php echo $num; ?>" <?php echo $style; ?> value="<?php echo $link_text; ?>"></td>
					<td>
					<select name="link_page<?php echo $num; ?>" >
					<option value='0'>Not Linked</option>
					<?php 
					
					$page_option=new Content_class();
					#$page_option->showPageOption($_SESSION['SITE_ID'],33);
					
					$page_option->showPageOption($link_page);
					if($link_page=="10") $product_page_selected="selected";
					else  $product_page_selected="";
					
					?>
					<option value='10' <?php echo  $product_page_selected; ?> >Product Page</option>
					</select></td>
					<td>
					<select name="link_parent<?php echo $num; ?>">
					<?php
					
						$page_option=new Content_class();
						
					#$page_option->showParentOption($_SESSION['SITE_ID'],6);
					$page_option->showPageOption($link_page);
					//$page_option->showParentOption($_SESSION['SITE_ID']);
					?>
					</select>					</td>
					<?php 
					if($link_active == "Y") $set="checked";
					else $set="";
					?>
					<td><input type='checkbox' value='Y' name="link_active<?php echo $num; ?>" <?php echo  $set; ?> ></td>
					<td><input name="link_position<?php echo $num; ?>" value="<?php echo $link_position; ?>" ></td>
	<td><a href="navbar.php?action=del&site_id=<?php echo $_SESSION['SITE_ID']; ?>&link_id=<?php echo $link_id; ?>">Delete</a></td>
					</tr>
					
					<?php
					$num=$num+1;
			}//NESTED WHILE.
		}//PARENT WHILE.
	
	/*if((isset($_REQUEST['linknum'])) && !(is_numeric($_REQUEST['linknum'])))
	{	
	//	echo"executed 1"; 
		$D=1; 
		newlink($D,$num,$_SESSION['SITE_ID']); 
		$D++;
		$num++; 
	}
	else if(!(isset($_REQUEST['linknum'])))
	{
		//echo"executed 2"; 
		$D=1; 
		newlink($D,$num,$_SESSION['SITE_ID']); 
		$D++;
		$num++; 
	}*/
	$D=1;
	if($_REQUEST['action']=="Add new link(s)") 
	{	
		#$num++;			
		for($D=1; $D<=$_REQUEST['linknum']; $D++)
		{
			#echo "*num : ".$num;
			#echo "*D : ".$D;
			newlink($D, $num++, $_SESSION['SITE_ID']);
			#$num++;			
		}
	}
?>

</TABLE>
<input type='hidden' name='num' value="<?php echo $num; ?>" />
<input type='hidden' name='D' value="<?php echo $D; ?>" />
</form>
<?php


	include_once("king_foot.php");
?>